Skip to content

通过DMA读取指定地址的字符串 - DmaReadString

函数简介

通过 DMA 读取指定地址的字符串,支持多种字符串编码格式和 CE 地址格式。(高级版功能,普通版无法使用)

接口名称

DmaReadString

DLL调用

c
OLA_STRING_RETURN OLA_CALL_TYPE DmaReadString(int64_t instance, int64_t deviceId, int32_t pid, OLA_STRING_INPUT addr, int32_t type, int32_t len);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
deviceId长整数型设备ID
pid整数型进程 PID
addr字符串地址,支持 CE 数据格式
type整数型字符串类型:0=GBK, 1=Unicode, 2=UTF8
len整数型读取长度(字节),0 表示自动判定

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
long deviceId = ola.DmaAddDevice(1);
// type/len 见文档,len 为读取字节数
string text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
long deviceId = ola.DmaAddDevice(1);
// type/len 见文档,len 为读取字节数
string text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 先添加 DMA 设备,返回的 deviceId 用于后续读写
deviceId = ola.DmaAddDevice(1)
# type/len 见文档,len 为读取字节数
text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
long deviceId = ola.DmaAddDevice(1);
// type/len 见文档,len 为读取字节数
String text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256);
cpp
var ola = com("OlaPlug.OlaSoft")
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
var deviceId = ola.DmaAddDevice(1)
// type/len 见文档,len 为读取字节数
var text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' 先添加 DMA 设备,返回的 deviceId 用于后续读写
deviceId = ola.DmaAddDevice(1)
' type/len 见文档,len 为读取字节数
text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256)
text
.局部变量 ola, OLAPlug
ola.创建 ()
' 先添加 DMA 设备,返回的 deviceId 用于后续读写
deviceId = ola.DmaAddDevice(1)
' type/len 见文档,len 为读取字节数
text = ola.DmaReadString(deviceId, 1234, “0x401000“, 0, 256)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
var deviceId = ola.DmaAddDevice(1);
// type/len 见文档,len 为读取字节数
var text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
长整数 deviceId = ola.DmaAddDevice(1)
// type/len 见文档,len 为读取字节数
文本型 text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 先添加 DMA 设备,返回的 deviceId 用于后续读写
long deviceId = ola.DmaAddDevice(1);
// type/len 见文档,len 为读取字节数
string text = ola.DmaReadString(deviceId, 1234, "0x401000", 0, 256);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long deviceId = DmaAddDevice(instance, 1);
long textPtr = DmaReadString(instance, deviceId, 1234, "0x401000", 0, 256);
if (textPtr != 0) {
    char text[512] = {0};
    GetStringFromPtr(textPtr, text, sizeof(text));
    FreeStringPtr(textPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long deviceId = DmaAddDevice(instance, 1);
long textPtr = DmaReadString(instance, deviceId, 1234, "0x401000", 0, 256);
if (textPtr != 0) {
    StringBuilder text = new StringBuilder(GetStringSize(textPtr) + 1);
    GetStringFromPtr(textPtr, text, text.Capacity);
    FreeStringPtr(textPtr);
    string textStr = text.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
deviceId = DmaAddDevice(instance, 1)
textPtr = DmaReadString(instance, deviceId, 1234, "0x401000", 0, 256)
if textPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(textPtr, buf, 512)
    ola.FreeStringPtr(textPtr)
    text = buf.value.decode("utf-8")

返回值

字符串指针,数据格式为 UTF-8 编码的字符串。

返回的字符串指针需要调用 FreeStringPtr 释放内存。